﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Util.SQLHelpers;

namespace AllSqlHelperWin.SQLHelperForms.OracleForms
{
    public partial class OracleUpdateForm : Form
    {
        /// <summary>
        /// 用户信息
        /// </summary>
        UserModel _userModel;

        public OracleUpdateForm()
        {
            InitializeComponent();
        }

        public OracleUpdateForm(UserModel userModel)
        {
            _userModel = userModel;
            InitializeComponent();
            cmbClass.SelectedIndex = 0;
        }

        private void MysqlUpdateForm_Load(object sender, EventArgs e)
        {
            txtName.Text = _userModel.Name;
            cmbClass.Text = _userModel.Class;
            txtAge.Text = _userModel.Age.ToString();
            if (_userModel.Gender)
            {
                rdBtnB.Checked = true;
            }
            else
            {
                rdBtnG.Checked = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("名称不可为空！");
                return;
            }

            string classStr = cmbClass.Text.Trim();
            string ageStr = txtAge.Text.Trim();
            int age = Convert.ToInt32(ageStr);

            bool gender = rdBtnB.Checked;

            //UserModel model = new UserModel()
            //{
            //    Name = name,
            //    Class = classStr,
            //    Age = string.IsNullOrEmpty(age) ? 0 : Convert.ToInt32(age),
            //    Gender = gender
            //};

            OracleParameter[] param = new OracleParameter[] {
               new OracleParameter("@Id",_userModel.Id),
               new OracleParameter("@Name",name),
               new OracleParameter("@Class",classStr),
               new OracleParameter("@Age",age),
               new OracleParameter("@Gender",gender),
               new OracleParameter("@CreateTime",DateTime.Now)  // 修改时间
            };

            int updeResult = OracleSqlHelper.ExecuteNonQuery("UPDATE User SET Name = @Name, Class = @Class, Age = @Age, Gender = @Gender, CreateTime = @CreateTime WHERE id = @Id", param);
            if (updeResult > 0)
            {
                MessageBox.Show("更新成功！");
            }
            else
            {
                MessageBox.Show("更新失败！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("名称不可为空！");
                return;
            }

            string classStr = cmbClass.Text.Trim();
            string ageStr = txtAge.Text.Trim();
            int age = Convert.ToInt32(ageStr);

            bool gender = rdBtnB.Checked;

            //UserModel model = new UserModel()
            //{
            //    Name = name,
            //    Class = classStr,
            //    Age = string.IsNullOrEmpty(age) ? 0 : Convert.ToInt32(age),
            //    Gender = gender
            //};

            OracleParameter[] param = new OracleParameter[] {
               new OracleParameter("@Id",_userModel.Id),
               new OracleParameter("@Name",name),
               new OracleParameter("@Class",classStr),
               new OracleParameter("@Age",age),
               new OracleParameter("@Gender",gender),
               new OracleParameter("@CreateTime",DateTime.Now)  // 修改时间
            };

            object updeResult = OracleSqlHelper.ExecuteScalar("UPDATE User SET Name = @Name, Class = @Class, Age = @Age, Gender = @Gender, CreateTime = @CreateTime WHERE id = @Id;select count(*) from User", param);
            if (updeResult != null)
            {
                MessageBox.Show($"更新成功！当前数据总量为：{Convert.ToInt32(updeResult)}");
            }
            else
            {
                MessageBox.Show("更新失败！");
            }
        }

        /// <summary>
        /// 只能输入数字
        /// </summary>
        private void txt1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果输入的不是退格和数字，则屏蔽输入
            if (!(e.KeyChar == 8 || (e.KeyChar >= 48 && e.KeyChar <= 57)))  // 小数点为46
            {
                e.Handled = true;
            }
        }
    }
}
